//
//  BaseAnimation.m
//  demo
//
//  Created by mysj on 16/7/7.
//  Copyright © 2016年 MY. All rights reserved.
//

#import "BaseAnimation.h"
#import <Masonry.h>


@interface BaseAnimation ()
/// 透明背景
@property (nonatomic, strong) UIView *coverView;
@end

@implementation BaseAnimation {
    
    BOOL    iSpresent;
}

- (UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
        _coverView.alpha = 0.0;
    }
    return _coverView;
}

#pragma mark - Animated Transitioning

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    
    UIView *containerView = [transitionContext containerView];
    UIView *p_view = nil;
    CGRect endFrame;
    if (iSpresent) {
        p_view = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
        //  [containerView addSubview:self.coverView]; 半透明背景
        //        self.coverView.frame = containerView.frame;
        
        p_view.translatesAutoresizingMaskIntoConstraints = NO;
        [containerView addSubview:p_view];
      
        switch (self.sizetype) {
            case kSizeTypeBp:
            {
                [p_view mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(@(containerView.frame.size.height * 0.3f));
                    make.left.right.equalTo(containerView);
                    make.bottom.equalTo(containerView);
                }];
            }
                break;
                case kSizeTypeEdges:
            {
                [p_view mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(containerView).offset(7);
                    make.left.right.equalTo(containerView).offset(7);
                    make.right.equalTo(containerView).offset(-7);
                    make.bottom.equalTo(containerView).offset(-7);
                    
                }];
            
            }
                break;
            default:
            {
                [p_view mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(containerView);
                }];
            }
                break;
        }
        
       
      
        
        endFrame = p_view.frame;
        p_view.frame = CGRectMake(endFrame.origin.x, containerView.frame.size.height, endFrame.size.width, endFrame.size.height);
        [containerView bringSubviewToFront:p_view];
        
    } else {
        
        UIView *disView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
        p_view = [disView snapshotViewAfterScreenUpdates:NO];
        p_view.frame = disView.frame;
        [containerView addSubview:p_view];
        [containerView bringSubviewToFront:p_view];
        [disView removeFromSuperview];
        
        endFrame = CGRectMake(p_view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, p_view.frame.size.width, p_view.frame.size.height);
    }
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0.0
         usingSpringWithDamping:0.8
          initialSpringVelocity:1.0
                        options:0
                     animations:^{
                         
                         p_view.frame = endFrame;
                     } completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
    
    //    {
    //
    //        UIView *modalView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
    //
    //
    //        UIView *snapshot = [modalView snapshotViewAfterScreenUpdates:NO];
    //        snapshot.frame = modalView.frame;
    //        [containerView addSubview:snapshot];
    //        [containerView bringSubviewToFront:snapshot];
    //        [modalView removeFromSuperview];
    //
    //        //Set the snapshot's anchor point for CG transform
    //        CGRect originalFrame = snapshot.frame;
    //        snapshot.layer.anchorPoint = CGPointMake(0.0, 1.0);
    //        snapshot.frame = originalFrame;
    //
    //        //Animate using keyframe animation
    //        [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{
    //            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.15 animations:^{
    //                //90 degrees (clockwise)
    //                snapshot.transform = CGAffineTransformMakeRotation(M_PI * -1.5);
    //            }];
    //            [UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.10 animations:^{
    //                //180 degrees
    //                snapshot.transform = CGAffineTransformMakeRotation(M_PI * 1.0);
    //                 _coverView.alpha = 0.0;
    //            }];
    //            [UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.20 animations:^{
    //                //Swing past, ~225 degrees
    //                snapshot.transform = CGAffineTransformMakeRotation(M_PI * 1.3);
    //
    //            }];
    //            [UIView addKeyframeWithRelativeStartTime:0.45 relativeDuration:0.20 animations:^{
    //                //Swing back, ~140 degrees
    //                snapshot.transform = CGAffineTransformMakeRotation(M_PI * 0.8);
    //            }];
    //
    //            [UIView addKeyframeWithRelativeStartTime:0.65 relativeDuration:0.35 animations:^{
    //                //Spin and fall off the corner
    //                //Fade out the cover view since it is the last step
    //                CGAffineTransform shift = CGAffineTransformMakeTranslation(180.0, 0.0);
    //                CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * 0.3);
    //                snapshot.transform = CGAffineTransformConcat(shift, rotate);
    //
    //            }];
    //        } completion:^(BOOL finished) {
    //            [_coverView removeFromSuperview];
    //            [containerView removeConstraints:_constraints];
    //            [transitionContext completeTransition:YES];
    //        }];
    //    }
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.duration) {
        return self.duration;
    }
    return 0.8f;
}


#pragma mark - Transitioning Delegate (Modal)
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    iSpresent = YES;
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    iSpresent = NO;
    return self;
}



@end
